package com.otod.cd.doc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.otod.base.service.BaseService;
import com.otod.cd.doc.constants.ShareConstant;
import com.otod.cd.doc.mapper.CdDocFolderMapper;
import com.otod.cd.doc.mapper.CdDocShareAuthMapper;
import com.otod.cd.doc.mapper.CdDocShareMapper;
import com.otod.cd.doc.vo.CdDocFolderVo;
import com.otod.cd.doc.vo.CdDocShareAuthVo;
import com.otod.commons.constants.LogConstant;
import com.otod.commons.utils.StringUtils;
import com.otod.pm.prj.vo.ProjectVo;
import com.otod.sys.emp.mapper.EmpMapper;
import com.otod.sys.vo.AuapREmpOrgRoleVo;

@Service
@Transactional(readOnly=true)
public class CdDocShareAuthService extends BaseService  {
	@Autowired
	CdDocShareAuthMapper cdDocShareAuthMapper;
	@Autowired
	CdDocShareMapper cdDocShareMapper;
	@Autowired
	CdDocFolderMapper cdDocFolderMapper;
	@Autowired
	EmpMapper empMapper;

	private static Logger logger = LoggerFactory.getLogger(CdDocShareAuthService.class);
	

	/**
	 * 通过ID查询文件信息
	 * @param uuid
	 * @return
	 */
	public CdDocShareAuthVo  getById(String uuid){
		if(StringUtils.isEmpty(uuid)){
			return null;
		}
		return cdDocShareAuthMapper.getById(uuid);
	}
	
	/**
	 * 新增
	 * @param cdDocShareAuthVo
	 * @return
	 */
	@Transactional
	public CdDocShareAuthVo insert(CdDocShareAuthVo cdDocShareAuthVo) {
		cdDocShareAuthVo.setUuid(super.getUUID());
		cdDocShareAuthMapper.insert(cdDocShareAuthVo);
		//日志
		super.invokeLogInsert( "400000000012", LogConstant.LOG_ADD, cdDocShareAuthVo.getUuid(),"新增分享");

		return cdDocShareAuthVo;
	}
	
	/**
	 * 通过 targetId查询相关数据
	 * @param targetId 
	 * @return
	 */
	public List<CdDocShareAuthVo> selectTargetId(String targetId) {
		List<CdDocShareAuthVo> list = Lists.newArrayList();
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("targetId", targetId);
		list = cdDocShareAuthMapper.queryForList(map);

		return list;
	}
	
	/**
	 * 查询得到文件夹
	 * @param targetId 目标的id 人员是empNo
	 * @param role 角色信息
	 * @param project 项目信息
	 * @return
	 */
	public List<CdDocFolderVo> queryForList(String targetId,List<AuapREmpOrgRoleVo> role,List<ProjectVo> project,String empNo){
		
		List<CdDocFolderVo> cdDocFolderVos = Lists.newArrayList();
		
		List<String> listRole= Lists.newArrayList();
		List<String> listProject = Lists.newArrayList();
		
		for (AuapREmpOrgRoleVo auapREmpOrgRoleVo : role) {
			listRole.add(auapREmpOrgRoleVo.getRoleId());
		}
		
		for (ProjectVo projectVo : project) {
			listProject.add(projectVo.getUuid());
		}
		
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("targetId", targetId);
		map.put("listRole", listRole);
		map.put("listProject",listProject);
	
	
		
		
		/**
		 * 去除当前用户
		 */
		List<CdDocFolderVo> cdDocFolderVos2 = Lists.newArrayList();
		cdDocFolderVos2 = cdDocFolderMapper.getShare(map);
	
		for (CdDocFolderVo cdDocFolderVo : cdDocFolderVos2) {
			
			if(!cdDocFolderVo.getCreateEmpNo().equals(empNo)){
				cdDocFolderVos.add(cdDocFolderVo);
			}
		}
		return cdDocFolderVos;
	}
	/**
	 * 通过shareId删除数据 没有使用in
	 * @param uuid
	 */
	@Transactional
	public void deleteShareId(String shareId){
		
		cdDocShareAuthMapper.deleteshareId(shareId);
		//日志
		super.invokeLogInsert( "400000000012", LogConstant.LOG_DELETE, shareId,"清空回收站");
	}
	
	/**
	 * 通过shareId删除数据 使用了in
	 * @param uuid
	 */
	@Transactional
	public void deleteshareIdJoin(String shareId){
		cdDocShareAuthMapper.deleteshareIdJoin(shareId);
		//日志
		super.invokeLogInsert( "400000000012", LogConstant.LOG_DELETE, shareId,"删除分享的数据");
	}
	/**
	 * 插入人员
	 * @param vo
	 */
	public void addPerson(String person,String uuid){
		String[] personSplit = person.split(";");
		CdDocShareAuthVo authVo=null;
		for (String personId : personSplit) {
			if(StringUtils.isNotEmpty(personId)){				
				authVo = new CdDocShareAuthVo();
				authVo.setUuid(super.getUUID());
				authVo.setShareId(uuid);
				authVo.setTargetType(ShareConstant.SHARE_GOAL_TYPE_PERSON);
				authVo.setTargetId(personId);
				cdDocShareAuthMapper.insert(authVo);
				//日志
				super.invokeLogInsert( "400000000012", LogConstant.LOG_ADD, authVo.getUuid(),"添加给分享的人");
			}
		}
		
	}
	/**
	 * 插入角色
	 * @param vo
	 */
	public void addRole(String role,String uuid){
		String[] roleSplit = role.split(";");
		CdDocShareAuthVo authVo=null;
		for (String roleId : roleSplit) {
			if(StringUtils.isNotEmpty(roleId)){
				authVo = new CdDocShareAuthVo();
				authVo.setUuid(getUUID());
				authVo.setShareId(uuid);
				authVo.setTargetType(ShareConstant.SHARE_GOAL_TYPE_ROLE);
				authVo.setTargetId(roleId);
				cdDocShareAuthMapper.insert(authVo);
				//日志
				super.invokeLogInsert( "400000000012", LogConstant.LOG_ADD, authVo.getUuid(),"添加给分享的角色");
			}
		}
		
	}
	/**
	 * 插入项目
	 * @param vo
	 */
	public void addProject(String project,String uuid){
		String[] projectSplit = project.split(";");
		CdDocShareAuthVo authVo=null;
		for (String projectId : projectSplit) {
			if(StringUtils.isNotEmpty(projectId)){
				authVo = new CdDocShareAuthVo();
				authVo.setUuid(getUUID());
				authVo.setShareId(uuid);
				authVo.setTargetType(ShareConstant.SHARE_GOAL_TYPE_PROJECT);
				authVo.setTargetId(projectId);
				cdDocShareAuthMapper.insert(authVo);
				//日志
				super.invokeLogInsert( "400000000012", LogConstant.LOG_ADD, authVo.getUuid(),"添加给分享的项目");	
			}
		}
	}


}
